<template>
  <div >
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import Cookies from "js-cookie";
import router from "@/router";

export default {
  name: "UpdatePass",
  data() {

    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: '',
        checkPass: '',

      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' },
          { required: true, message: "密码必填", trigger: "blur" },
          { min: 8, max: 20, message: "密码在8到20个字符之间", trigger: "blur" }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' },
          { required: true, message: "密码必填", trigger: "blur" },
          { min: 8, max: 20, message: "密码在8到20个字符之间", trigger: "blur" }
        ],
      }
    };
  },
  methods: {
    initCurrentUser() {
      let usertoken  = Cookies.get('user');
      console.log("usertoken/////////////")
      console.log(usertoken)

      const config = {
        token:usertoken,
      }
      console.log("config////////////////////////////////////////////")
      console.log(config)
      this.$axios.post("/userinfo/analysis", config)
          .then(response => {
            console.log(response)

            let responseData = response.data;
            console.log(responseData)


            if (responseData.code === 200) {
              let userdata = responseData.data;
              console.log( "userdata---------------------------------")
              console.log( userdata)

              if (userdata) {
                this.currentUserInfo = userdata;
                this.currentSrc = userdata.userIcon;
                console.log( "this.currentSrc-------------------------")
                console.log( this.currentSrc)
              }else {
                this.$message({
                  message: responseData.msg,
                  type: "error",
                  duration: 2000,
                })
                this.logout();
              }
            } else {
              this.$message({
                message: "登录状态异常,请重新登录",
                type: "error",
                duration: 2000,
              })
              this.logout();
            }
            this.src = response.data.data.userIcon;
          })

    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          // alert(this.ruleForm.pass)
          console.log(this.ruleForm.pass)
          console.log(this.ruleForm.checkPass)
          // alert(this.ruleForm.checkPass)
          let param={
            config1:this.ruleForm.pass,
            config2:this.ruleForm.checkPass,
          }
          this.$axios.post("/userinfo/updateUserPass",param).then(function (res){
            console.log(res);
            let responseData = res.data;
            if (responseData.code === 200) {
              alert("修改密碼成功")
              router.push("/Success")
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {
    this.initCurrentUser();
  }
}
</script>

<style scoped>

</style>
